package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.ReceiveStatusBean;

import java.util.ArrayList;
import java.util.List;

public class X3StatusChangeAdapter extends RecyclerView.Adapter<X3StatusChangeAdapter.MileChangeHolder> {

    private List<ReceiveStatusBean> mList;

    private Context mContext;

    private List<Boolean> sList;

    public X3StatusChangeAdapter(Context mContext ) {
        this.mList = new ArrayList<>();
        this.mContext = mContext;
        this.sList = new ArrayList<>();

    }

    public void changeData(List<ReceiveStatusBean> mList) {
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MileChangeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MileChangeHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_status_change_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MileChangeHolder holder, int position) {
        if (sList.get(position)) {
            holder.iv_select.setImageResource(R.mipmap.x3_unit_select);
        }else {
            holder.iv_select.setImageResource(R.mipmap.x3_unit_default);
        }
        holder.tv_title.setText(mList.get(position).statusName + "");

        holder.itemView.setOnClickListener(view -> {
            for (int i = 0; i < sList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == position);
            }
            notifyDataSetChanged();
            if (iOnClickUnit != null) {
                iOnClickUnit.iOnClickChangeUnit(mList.get(position).id);
            }
        });
    }

    IOnClickUnit iOnClickUnit;

    public void setiOnClickUnit(IOnClickUnit iOnClickUnit) {
        this.iOnClickUnit = iOnClickUnit;
    }

    public interface IOnClickUnit {
        void iOnClickChangeUnit(long id);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MileChangeHolder extends RecyclerView.ViewHolder {
        ImageView iv_select;
        TextView tv_title;
        View v_status;
        public MileChangeHolder(@NonNull View itemView) {
            super(itemView);
            iv_select = itemView.findViewById(R.id.iv_select);
            tv_title = itemView.findViewById(R.id.tv_title);
            v_status = itemView.findViewById(R.id.v_status);
        }
    }
}
